package cn.qqcn.user.controller;

import cn.qqcn.common.vo.Result;
import cn.qqcn.user.entity.Dept;
import cn.qqcn.user.entity.Emp;
import cn.qqcn.user.service.EmpService;
import cn.qqcn.vo.EmpQuery;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class EmpController {

    @Resource
    private EmpService empService;

    @GetMapping("/emp")
    public String toEmpListUI() {
        return "/emp/empList";
    }

    @GetMapping("/list")
    @ResponseBody
    public Result<Object> getEmpList(EmpQuery param) {
        List<Emp> list = empService.getEmpList(param);
        Long count = empService.countEmpList(param);
        return Result.success(list, count);
    }

    @PostMapping("/emp")
    @ResponseBody
    public Result<Object> addEmp(Emp emp) {
        empService.addEmp(emp);
        return Result.success("添加用户成功");
    }
    @GetMapping("/add/ui")
    public String toAddUi(Model model) {
        List<Dept> deptList = empService.getAllDept();
        model.addAttribute("deptList", deptList);
        return "/emp/empAdd";
    }
    @DeleteMapping("/{ids}")
    @ResponseBody
    public Result<Object> deleteEmpByIds(@PathVariable("ids") String ids) {
        empService.deleteEmpByIds(ids);
        System.out.println("ids是" + ids);
        return Result.success("删除用户成功");
    }


    @RequestMapping("/getOne/{id}")
    public String getEmpById(@PathVariable("id") Integer id, Model model) {
        Emp emp = empService.getEmpById(id);
        model.addAttribute("emp", emp);
        model.addAttribute("deptList", empService.getAllDept());
        return "/emp/empEdit";
    }

    @PutMapping("/update")
    @ResponseBody
    public Result<Object> updateEmp(Emp emp) {
        empService.updateEmp(emp);
        System.out.println("特工信息修改成功");
        return Result.success("特工信息修改成功！");
    }
}
